
var goals = ['make base','make ship','attack','mine'];

function brain(api)
{
	//properties
	this.api = api;
	
	//functions
	this.think = function think()
	{
		console.log('thinking');
		console.groupCollapsed('thinking for a base');
		for(var i in this.api.mybases)
		{
			var base = this.api.mybases[i];
			console.log(base.idx);
			this.create(base);
		}
		console.groupEnd();
		
		console.groupCollapsed('thinking for a ships');
		for(var i in this.api.myships)
		{
			var ship = this.api.myships[i];
			
			console.log(ship.idx);
			var n = this.attack(ship,this.api.otherships);
			if(n == null)
			{
				n = this.mine(ship,this.api.asteroids);
				if(n == null)
				{
					n = this.move(ship,this.api.asteroids);
					if(n == null)
					{
						this.explore(ship);
					}
				}
			}
		}
		console.groupEnd();
		
	};
	
	this.nearestbase = function nearestbase(x,y)
	{
		return this.nearest(x,y,this.api.mybases);
	}
	
	this.create = function create(base)
	{
		if(base.ore > 1000)
		{
			var x= Math.floor(Math.random()*900);
			var y= Math.floor(Math.random()*400);
			
			this.api.add_create(x,y,'base',base.idx);
		}
		else if(base.ore > 10)
		{
			this.api.add_create(base.x,base.y,'ship',base.idx);
		}
	};
	
	this.attack = function attack(ship,otherships)
	{
		var nearest = this.nearest(ship.x,ship.y,otherships);
		if(nearest != null && nearest.distance < ship.range)
		{
			this.api.add_attack(ship.idx,nearest.idx);
			return nearest;
		}
		return null;
	};
	
	this.mine = function mine(ship,asteroids)
	{
		var nearest = this.nearest(ship.x,ship.y,asteroids);
		if(nearest != null && nearest.distance < ship.range)
		{
			var nearbase = this.nearestbase(ship.x,ship.y)
			this.api.add_mine(ship.idx,nearest.idx,nearbase.idx);
			return nearest;
		}
		return null;
	};
	
	this.move = function move(ship,asteroids)
	{
		var nearest = this.nearest(ship.x,ship.y,asteroids);
		if(nearest != null)
		{
			var loc = this.moveto(ship,nearest.x,nearest.y);
			this.api.add_move(loc.x,loc.y,ship.idx);
			return nearest;
		}
		return null;
	};
	
	this.explore = function explore(ship)
	{
		var loc = this.moveto(ship,100,100);
		this.api.add_move(loc.x,loc.y,ship.idx);
	};
	
	this.moveto = function moveto(ship,x2,y2)
	{
	    diff_x = x2 - ship.x;
	    diff_y = y2 - ship.y;
	    atan2 = Math.atan2(diff_y, diff_x);
	    cos = Math.cos(atan2);
	    sin = Math.sin(atan2);
	    new_x = parseInt(Math.round(ship.x + cos * (ship.speed - 1)));
	    new_y = parseInt(Math.round(ship.y + sin * (ship.speed - 1)));
	    return {x:new_x,y:new_y};
	};
	
	this.nearest = function nearest(x,y,search_list)
	{
		var nearest = null;
		for(var i in search_list)
		{
			var x2 = search_list[i].x;
			var y2 = search_list[i].y;
			var this_distance = this.distance(x,y,x2,y2);
			if(nearest == null)
			{
				nearest = search_list[i];
				nearest.distance = this_distance;
			}
			else if(this_distance < nearest.distance)
			{
				nearest = search_list[i];
				nearest.distance = this_distance;
			}
		}
		
		return nearest;
	};
	
	this.distance = function distance(x1,y1,x2,y2)
	{
		var diffx = x2 - x1;
		var diffy = y2 - y1;
		return Math.sqrt(diffx * diffx + diffy * diffy);
	};
}


















